<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');


class signup extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->template->set_template('login');
		$this->load->library('form_validation');
		parent::add_jQueryMobile_css();
		parent::add_jQueryMobile_js();
		$this->load->model("user_accounts_model", "user_model");
	}

	public $data = array();

	public function index($signup_info = array(), $isError = false, $isPostBack = false)
	{
		if($this->_is_connect()){
			redirect(site_url('users'), 'refresh');
			return TRUE;
		}
		else {
				$this->form_validation->set_rules('email', 'Email', 'trim|xss_clean|email|callback_email_check');
				$this->form_validation->set_rules('passwd', $this->lang->line('passwd'), 'trim|xss_clean|callback_passwd_check');
				$this->form_validation->set_rules('repasswd', $this->lang->line('passwd'), 'trim|xss_clean|callback_repasswd_check');

				$this->form_validation->set_error_delimiters('<em style="color:red">','</em>');

				$data['email'] = $this->input->post('email');
				$data['passwd'] = $this->input->post('passwd');
				$data['repasswd'] = $this->input->post('repasswd');
				$data['signup_url'] = site_url('signup');

				if($this->form_validation->run() == FALSE)
				{
						
					$this->template->parse_view("content", "view_signup", $data);
					$this->template->render();
				}
				else
				{
					$this->session->set_userdata('email', $data['email']);
					$this->user_model->add($data);
					redirect('users', 'refresh');

				}
		
		}
	}



	public function email_check($email){
		if(!isset($email)){
			$this->form_validation->set_message('email_check', $this->lang->line('email_required'));
			return FALSE;
		}

		$exist_user = $this->user_model->_check_user_exist($email);

		if ($exist_user)
		{
			$this->form_validation->set_message('email_check', $this->lang->line('email_existed'));
			return FALSE;
		}
	}

	public function passwd_check($pwd){
		if(!isset($pwd)){
			$this->form_validation->set_message('passwd_check', $this->lang->line('pwd_required'));
			return FALSE;
		}else{
			$data['passwd'] = $pwd;
		}
	}

	public function repasswd_check($pwd){
		if(!isset($pwd)){
			$this->form_validation->set_message('repasswd_check', $this->lang->line('repwd_required'));
			return FALSE;
		}else{
			if($this->input->post('passwd') != $this->input->post('repasswd')){
				$this->form_validation->set_message('repasswd_check', $this->lang->line('match_pwd_required'));
				return FALSE;
			}
		}
	}



	public function sign_up()
	{
		$email = $this->input->post('email');
		$passwd = $this->input->post('passwd');
		$repasswd = $this->input->post('repasswd');

		if(empty($email)){
			$this->form_validation->set_message('email', $this->lang->line('email_empty'));
			return FALSE;
		}

	}
}

?>